package com.example.apple.ddhr_new;

import android.Manifest;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.activity.App;
import com.activity.Config;
import com.activity.fragment.BeautyFragment;
import com.activity.fragment.HomeFragment;
import com.activity.fragment.HomeNewFragment;
import com.activity.fragment.MessageFragment;
import com.activity.fragment.ShopFragment;
import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.OSSConstants;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSFederationToken;
import com.alibaba.sdk.android.oss.common.utils.IOUtils;
import com.api.LoginApi;
import com.api.viewModel.LoginVM;

import com.baidu.idl.face.platform.FaceConfig;
import com.baidu.idl.face.platform.FaceEnvironment;
import com.baidu.idl.face.platform.FaceSDKManager;
import com.baidu.idl.face.platform.LivenessTypeEnum;
import com.example.http.BuildFactory;
import com.example.lzbase.BaseViewModelActivity;
import com.gyf.barlibrary.ImmersionBar;


import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseViewModelActivity <LoginVM> {



    @BindView(R.id.frame_layout)
    protected FrameLayout frameLayout;
    @BindView(R.id.radio_group)
    protected RadioGroup tabbar;

    Fragment currentFragment;
    private FragmentManager manager;

    private HomeNewFragment homeFragment;
    private MessageFragment applicationFragment;
    private BeautyFragment beautyFragment;
    private ShopFragment mineFragment;
    private HomeFragment settingFragment;
    @Override
    protected int titleText() {
        return R.string.app_name;
    }




    @Override
    public void initStatusBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .init();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {



        super.created(savedInstanceState);
            manager = getSupportFragmentManager();

            tabbar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {

                    if(checkedId!=R.id.setting)
                    {
                        return;
                    }

                    FragmentTransaction ft1 = manager.beginTransaction();
                    if(currentFragment!=null)
                    {
                        ft1.hide(currentFragment);
                    }
                    if(checkedId==R.id.home){


                        if (homeFragment ==null){
                            homeFragment = new HomeNewFragment();
                            ft1.add(R.id.frame_layout, homeFragment);


                        }
                        currentFragment = homeFragment;
                        ft1.show(homeFragment);
                        ft1.commit();


                    }else if(checkedId==R.id.app){

                        if (applicationFragment ==null){
                            applicationFragment = new MessageFragment();
                            ft1.add(R.id.frame_layout, applicationFragment);

                        }
                        currentFragment = applicationFragment;
                        ft1.show(applicationFragment);
                        ft1.commit();

                    }else if(checkedId==R.id.meili){

                        if (beautyFragment ==null){
                            beautyFragment = new BeautyFragment();
                            ft1.add(R.id.frame_layout, beautyFragment);

                        }
                        currentFragment = beautyFragment;
                        ft1.show(beautyFragment);
                        ft1.commit();

                    }else if(checkedId==R.id.mine){

                        if (mineFragment ==null){
                            mineFragment = new ShopFragment();
                            ft1.add(R.id.frame_layout, mineFragment);

                        }
                        currentFragment = mineFragment;
                        ft1.show(mineFragment);
                        ft1.commit();
                    }else {

                        if (settingFragment ==null){
                            settingFragment = new HomeFragment();
                            ft1.add(R.id.frame_layout, settingFragment);

                        }
                        currentFragment = settingFragment;
                        ft1.show(settingFragment);
                        ft1.commit();
                    }
                }
            });
        ((RadioButton)tabbar.findViewById(R.id.setting)).setChecked(true); //check(R.id.home);

        viewModel.oss.observe(this,res->{
            OSSCredentialProvider credetialProvider = new OSSFederationCredentialProvider() {
                @Override
                public OSSFederationToken getFederationToken() {
                    try {

                        return new OSSFederationToken(res.getAccesskeyid(), res.getAccesskeysecret(), res.getSecuritytoken(), res.getExpiration());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            };
            OSSClient ossClient = new OSSClient(this,"oss-cn-shanghai.aliyuncs.com",credetialProvider,new ClientConfiguration());
            App.getInstance().oss = ossClient;

        });
        viewModel.ossError.observe(this,res->{
            addSubscription(viewModel.getOss());
        });

        addSubscription(viewModel.getOss());

        FaceSDKManager.getInstance().initialize(this, Config.licenseID, Config.licenseFileName);

        FaceConfig config = FaceSDKManager.getInstance().getFaceConfig();

        ArrayList<LivenessTypeEnum> livenessList =new ArrayList<>();
        livenessList.add(LivenessTypeEnum.Eye);
        livenessList.add(LivenessTypeEnum.Mouth);
        livenessList.add(LivenessTypeEnum.HeadUp);
        livenessList.add(LivenessTypeEnum.HeadDown);
        livenessList.add(LivenessTypeEnum.HeadLeft);
        livenessList.add(LivenessTypeEnum.HeadRight);
        livenessList.add(LivenessTypeEnum.HeadLeftOrRight);
        // SDK初始化已经设置完默认参数（推荐参数），您也根据实际需求进行数值调整
        config.setLivenessTypeList(livenessList);
        config.setLivenessRandom(false);
        config.setSound(false);
        config.setBlurnessValue(FaceEnvironment.VALUE_BLURNESS);
        config.setBrightnessValue(FaceEnvironment.VALUE_BRIGHTNESS);
        config.setCropFaceValue(FaceEnvironment.VALUE_CROP_FACE_SIZE);
        config.setHeadPitchValue(FaceEnvironment.VALUE_HEAD_PITCH);
        config.setHeadRollValue(FaceEnvironment.VALUE_HEAD_ROLL);
        config.setHeadYawValue(FaceEnvironment.VALUE_HEAD_YAW);
        config.setMinFaceSize(FaceEnvironment.VALUE_MIN_FACE_SIZE);
        config.setNotFaceValue(FaceEnvironment.VALUE_NOT_FACE_THRESHOLD);
        config.setOcclusionValue(FaceEnvironment.VALUE_OCCLUSION);
        config.setCheckFaceQuality(true);
        config.setFaceDecodeNumberOfThreads(2);

        FaceSDKManager.getInstance().setFaceConfig(config);

        BuildFactory.getBaiduService(LoginApi.class).getToken().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(res -> {
                    App.getInstance().authBean = res;



                },throwable -> {

                    onError(throwable);

                });

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        super.onPermissionsGranted(requestCode, perms);
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        super.onPermissionsDenied(requestCode, perms);
    }


    public void check(int index){
        if(index==1){
//            tabbar.check(R.id.app);
            ((RadioButton)tabbar.findViewById(R.id.app)).setChecked(true);

//            applicationFragment.scrollBottom();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
